<?php
include 'checklogin.php';
//ajax 投票处理
if(!empty($_POST)){
    if(empty($_POST['id'])){
        echo json_encode(array('code'=>false,'msg'=>'参数错误'));exit;
    }
    
    //进行投票操作
    //连接数据库
    $link = mysql_connect('localhost', 'root', '');
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }else{
        mysql_select_db('test', $link) or die('Could not select database.');
        mysql_query("set names 'utf8'",$link);
    }
    $date = date('Y-m-d').' 00:00:00';
    //验证今天是否投过票
    $logSql = mysql_query("SELECT * FROM vote_logs WHERE create_id={$_SESSION['uid']} AND create_time>'$date'");
    $logs = mysql_fetch_array($logSql);
    if(!empty($logs)){
        echo json_encode(array('code'=>false,'msg'=>'你今天已投过票了哦'));exit;
    }

    $date = date('Y-m-d H:i:s');
    //记录投票日志
    $sql = mysql_query("INSERT INTO vote_logs(`vote_id`,`create_id`,`create_time`)values({$_POST['id']},{$_SESSION['uid']},'$date')");
    //修改投票总数
    $sql = mysql_query("update vote_infos set vote_num=vote_num+1 where id = {$_POST['id']}");
    if($sql){
        echo json_encode(array('code'=>true,'msg'=>'投票成功'));exit;
    }
    echo json_encode(array('code'=>false,'msg'=>'投票失败'));exit;
}

if(empty($_GET['id'])){
    exit('请求错误');
}
$id = $_GET['id'];
//连接数据库
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}else{
    mysql_select_db('test', $link) or die('Could not select database.');
    mysql_query("set names 'utf8'",$link);
}

$info = mysql_query("select * from vote_infos where topic_id={$_GET['id']}");
$row = array();
if($info){
    while ($row = mysql_fetch_array($info,MYSQL_ASSOC)) {
        $result[] =  $row;
    }
}
include 'voteinfo.html';


function pr($data){
    print('<pre>');
    print_r($data);
    print('</pre>');
}
?>